package com.codingman.struts2demo3;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class LoginAction extends ActionSupport implements Preparable, ModelDriven<User> {
	private User user = new User();

	public String execute() {
		System.out.println("execute ...");
		try {
			Thread.sleep(2000);
		} catch (Exception e) {
		}

		if (user.getPassword().equals("admin")) {
			return SUCCESS;
		} else
			return ERROR;
	}

	public void validate() {
		if (user.getName().length() < 1)
			addFieldError("name", "Name can't be blank");
		if (user.getPassword().length() < 1)
			addFieldError("password", "Password can't be blank");
	}

	@Override
	public User getModel() {
		System.out.println("getModel ...");
		return user;
	}

	@Override
	public void prepare() throws Exception {
		System.out.println("prepare ...");
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		System.out.println("setUser ...");
		this.user = user;
	}
}
